package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.business.entity.YytBaseCatalog;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.service.YytBaseCatalogService;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.oms.entity.YytOmsCartItem;
import com.ruoyi.common.oms.service.YytOmsCartItemService;
import com.ruoyi.common.pms.entity.YytPmsCategoryBrandRelation;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.pms.service.YytPmsCategoryBrandRelationService;
import com.ruoyi.common.pms.service.YytPmsSkuInfoService;
import com.ruoyi.utils.LoginUtils;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import com.ruoyi.vo.YytCatalogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CatalogController
 * @Description
 * @Author Jieluo
 * @Date 2021/12/23 15:55
 */
@RestController
@RequestMapping("business")
public class CatalogController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private YytBaseCatalogService yytBaseCatalogService;

    @Autowired
    private YytPmsCategoryBrandRelationService yytPmsCategoryBrandRelationService;

    @Autowired
    private YytPmsSkuInfoService yytPmsSkuInfoService;

    @Autowired
    private YytOmsCartItemService yytOmsCartItemService;

    @Autowired
    private YytMemberService yytMemberService;



//    @GetMapping("catalogList")
    public   ResponseResult catalogList(){
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<YytBaseCatalog> list = yytBaseCatalogService.list(wrapper);
        return ResponseResult.success(list);
    }
    @GetMapping("twoCatalogList")
    public ResponseResult twoCatalogList(HttpServletRequest request){
        String id = request.getParameter("id");
        System.out.println("id"+id);
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<YytBaseCatalog> list = yytBaseCatalogService.list(wrapper);
        return ResponseResult.success(list);
    }
    @GetMapping("threeCatalogList")
    public ResponseResult threeCatalogList(HttpServletRequest request){
        String id = request.getParameter("id");
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.like("cat_level",id);
        List<YytBaseCatalog> list = yytBaseCatalogService.list(wrapper);
        return ResponseResult.success(list);
    }

    /**
     * 修改新代码
     * @return
     */
    @GetMapping("/catalogList")
    public ResponseResult newCatalogList(){
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<YytBaseCatalog> list = yytBaseCatalogService.list(wrapper);
        List<YytCatalogVo> resultList = extracted(list);
        return ResponseResult.success(resultList);
    }

    // 抽取出来的方法
    private List<YytCatalogVo> extracted(List<YytBaseCatalog> list) {
        List<YytCatalogVo> result = new ArrayList<>();
        for (YytBaseCatalog yb: list){
            QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
            YytCatalogVo yytCatalogVo = new YytCatalogVo();
            wrapper.eq("parent_id", yb.getId());
            List<YytBaseCatalog> list1 = yytBaseCatalogService.list(wrapper);
            List<YytCatalogVo> extracted = extracted(list1);
            BeanUtils.copyProperties(yb, yytCatalogVo);
            yytCatalogVo.setChildren(extracted);
            result.add(yytCatalogVo);
        }

        return result;
    }

    /**
     * 获取新的分类
     * @return
     */
    @GetMapping("/getNewCatalogList")
    public ResponseResult getNewCatalogList(){
        List<YytBaseCatalog> list = yytBaseCatalogService.list();
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.ne("parent_id", 0).groupBy("parent_id");
        List<YytBaseCatalog> list1 = yytBaseCatalogService.list(wrapper);
        for (int i = 0; i < list.size(); i++){
            for (YytBaseCatalog yytBaseCatalog:list1){
                if(list.get(i).getId().equals(yytBaseCatalog.getParentId())){
                    list.remove(i);
                }
            }
        }
        return ResponseResult.success(list);
    }

    /**
     * 获取品牌数据
     * @param catalogId
     * @return
     */
    @GetMapping("/getBrandList")
    public ResponseResult getBrandList(@RequestParam("catalogId") String catalogId){
        QueryWrapper<YytPmsCategoryBrandRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catalogId);
        return ResponseResult.success(yytPmsCategoryBrandRelationService.list(wrapper));
    }

    /**
     * 获取商品分类边栏
     * @param catalogId
     * @return
     */
    @GetMapping("/getChildNode")
    public ResponseResult getChildNode(@RequestParam("catalogId") String catalogId) {
        return ResponseResult.success(yytBaseCatalogService.geiChildNode(catalogId));
    }

    /**
     * 根据分类第一级 id 获取所有子节点
     * @param catalogId
     * @return
     */
    @GetMapping("/getChildNodeAll")
    public ResponseResult getChildNodeAll(@RequestParam("catalogId") String catalogId){
        return ResponseResult.success(yytBaseCatalogService.getChildNodeAll(catalogId));
    }

    /**
     * 根据分类id获取商品
     * @param classId
     * @return
     */
    @GetMapping("/getClassIdBySkuInfo")
    public ResponseResult getClassIdBySkuInfo(@RequestParam("classId") String classId, @RequestParam("sortValue") Integer sortValue, @RequestParam("wechatId") String wechatId , @RequestParam("sqType") Integer sqType){
        String sortName = sortValue == 0?"no":(sortValue == 1?"sale_count desc":"price");
        QueryWrapper<YytMember> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat_id", wechatId);
        YytMember one = yytMemberService.getOne(wrapper, false);
        System.out.println("one = " + one);
        if (one == null){
            return ResponseResult.success(yytPmsSkuInfoService.getClassIdSkuInfo(classId, sortName, null, sqType));
        }
        return ResponseResult.success(yytPmsSkuInfoService.getClassIdSkuInfo(classId, sortName, one.getId(), sqType));
    }

    /**
     * 根据子节点获取数据
     * @param classId
     * @return
     */
    @GetMapping("/getChildData")
    public ResponseResult getChildData(@RequestParam("classId") String classId, @RequestParam("sortValue") Integer sortValue, @RequestParam("wechatId") String wechatId, @RequestParam("sqType") Integer sqType){
        String sortName = sortValue == 0?"no":(sortValue == 1?"sale_count":"price");
        QueryWrapper<YytMember> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat_id", wechatId);
        YytMember one = yytMemberService.getOne(wrapper, false);
        if (one == null){
            return ResponseResult.success(yytPmsSkuInfoService.getChildData(classId, sortName, null, sqType));
        }
        return ResponseResult.success(yytPmsSkuInfoService.getChildData(classId, sortName, one.getId(), sqType));
    }

    /**
     * 获取当前用户的购物车数量及总金额
     * @return
     */
    @GetMapping("/getCartNumAndMoney")
    public ResponseResult getCartNumAndMoney(){
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        return ResponseResult.success(yytOmsCartItemService.getCartNumAndMoney(loginUser.getId()));
    }

    /**
     * 该商品是否已经添加到购物车
     * @param skuId
     * @return
     */
    @GetMapping("/getSkuInfoIsNull")
    public ResponseResult getSkuInfoIsNull(@RequestParam("skuId") String skuId){
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        QueryWrapper<YytOmsCartItem> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", loginUser.getId()).eq("del_flag", 0).eq("product_sku_id", skuId);
        YytOmsCartItem one = yytOmsCartItemService.getOne(wrapper, true);
        if (one == null){
            return ResponseResult.success(false);
        }
        return ResponseResult.success(one);
    }

    /**
     * 清除购物车某条数据
     * @param id
     * @return
     */
    @GetMapping("/removeCartShop")
    public ResponseResult removeCartShop(@RequestParam("cartId") String id) {
        boolean b = yytOmsCartItemService.removeById(id);
        return ResponseResult.success(b);
    }

    /**
     * 获取商城自营三级分类
     * @param catalogType
     * @return
     */
    @GetMapping("/getCatalogList")
    public ResponseResult getCatalogList(@RequestParam("catalogType") Integer catalogType){
        QueryWrapper<YytBaseCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("catalog_type", catalogType).like("cat_level", ",");
        return ResponseResult.success(yytBaseCatalogService.list(wrapper));
    }


}
